/**
 * PathLayout.java
 * com.jamin.path
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-4 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.custom_view_topbar_path;

import com.jamin.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

/**
 * ClassName:PathLayout
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-4 下午2:28:57 
 */
public class PathLayout extends RelativeLayout{

	private static final String TAG = "PathLayout========wangjieming";
	Context context;



	//Attr
	private Drawable openBackground,openLayoutBackground;
	private Drawable firstBackground,secondBackground,thirdBackground,fourthBackground;
	private int firstVisibility,secondVisibility,thirdVisibility,fourthVisibility;

	//OpenLayout
	RelativeLayout mOpenLayout;
	ImageView mOpenBtn;
	//FuncLayout
	RelativeLayout mFuncLayout;
	ImageButton mFirstFuncBtn , mSecondFuncBtn , mThirdFuncBtn , mFourthFuncBtn;//from right to left
	//ClickListener
	PathClickListener pathClickListener;

	private boolean areButtonsShowing;



	public PathLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		MyAnimations.initOffset(context);
		//Get XML Attrs
		getTypeArray(attrs);
		//initPathLayout
		initPathLayout();
		//OpenLayout
		initOpenView();
		//FuncBtnLayout
		initFuncBtnLayout();
	}



	/**
	 * Init PathLayout<br>
	 * @method initPathLayout
	 * @throws 
	 * @since v1.0
	 */
	private void initPathLayout() {
		LayoutParams pathLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		setClipChildren(false);
		setClipToPadding(false);
		setLayoutParams(pathLayoutParams);
		setPadding(0, 0, 0, dip2px(context, 10));
	}



	/**
	 * Init Open View<br>
	 * @method initOpenView
	 * @throws 
	 * @since v1.0
	 */
	private void initOpenView() {
		mOpenLayout = new RelativeLayout(context);
		mOpenLayout.setBackgroundDrawable(openLayoutBackground);
		LayoutParams mOpenLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mOpenLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		mOpenLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
		mOpenLayoutParams.setMargins(0, 0, 15, 15);//左上右下
		//open Btn
		mOpenBtn = new ImageView(context);
		mOpenBtn.setImageDrawable(openBackground);
		mOpenBtn.setClickable(true);
		mOpenBtn.setPadding(5, 5, 5, 5);//expand btn size
		LayoutParams mOpenbtnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mOpenbtnParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		mOpenLayout.addView(mOpenBtn , mOpenbtnParams);
		//打开按钮加到Path布局中
		addView(mOpenLayout , mOpenLayoutParams);
		mOpenBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!areButtonsShowing) {
					Log.d( TAG , "展开");
					MyAnimations.startAnimationsIn(mFuncLayout, 300);
					mOpenBtn.startAnimation(MyAnimations.getRotateAnimation(0, 225 , 300));
				} else {
					Log.d( TAG , "关闭");
					MyAnimations.startAnimationsOut(mFuncLayout, 300);
					mOpenBtn.startAnimation(MyAnimations.getRotateAnimation(225, 360, 300));
				}
				areButtonsShowing = !areButtonsShowing;
			}
		});
	}


	private void initFuncBtnLayout() {
		//Layout
		mFuncLayout = new RelativeLayout(context);
		LayoutParams mFuncLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mFuncLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		mFuncLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
		//FirstBtn
		mFirstFuncBtn = new ImageButton(context);
		mFirstFuncBtn.setBackgroundDrawable(firstBackground);
		mFirstFuncBtn.setVisibility(firstVisibility);
		LayoutParams mFirstBtnLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
		mFirstBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		mFirstBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
		mFirstBtnLayoutParams.setMargins(0, dip2px(context, 60), dip2px(context, 10) , 0);//左上右下
		mFuncLayout.addView(mFirstFuncBtn, mFirstBtnLayoutParams);
		//secondBtn
		mSecondFuncBtn = new ImageButton(context);
		mSecondFuncBtn.setBackgroundDrawable(secondBackground);
		mSecondFuncBtn.setVisibility(secondVisibility);
		LayoutParams mSecondBtnLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
		mSecondBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		mSecondBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
		mSecondBtnLayoutParams.setMargins(0, dip2px(context, 60), dip2px(context, 58) , 0);//左上右下
		mFuncLayout.addView(mSecondFuncBtn, mSecondBtnLayoutParams);
		//thirdBtn
		mThirdFuncBtn = new ImageButton(context);
		mThirdFuncBtn.setBackgroundDrawable(thirdBackground);
		mThirdFuncBtn.setVisibility(thirdVisibility);
		LayoutParams mThirdBtnLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
		mThirdBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
		mThirdBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
		mThirdBtnLayoutParams.setMargins(0, dip2px(context, 60), dip2px(context, 108) , 0);//左上右下
		mFuncLayout.addView(mThirdFuncBtn, mThirdBtnLayoutParams);
		addView(mFuncLayout, mFuncLayoutParams);
		mFirstFuncBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MyAnimations.startScaleAnimation(mFirstFuncBtn, 150);
				if(pathClickListener != null){
					pathClickListener.firstBtnClick();
				}
			}
		});
		mSecondFuncBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MyAnimations.startScaleAnimation(mSecondFuncBtn, 150);
				if(pathClickListener != null){
					pathClickListener.secondBtnClick();
				}
			}
		});
		mThirdFuncBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				MyAnimations.startScaleAnimation(mThirdFuncBtn , 150);
				if(pathClickListener != null){
					pathClickListener.thirdBtnClick();
				}
			}
		});
	}



	/**
	 * get XML attr<br>
	 * @method getTypeArray
	 * @param attrs
	 * @throws 
	 * @since v1.0
	 */
	private void getTypeArray(AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.path);
		openBackground = ta.getDrawable(R.styleable.path_openbackground);
		openLayoutBackground = ta.getDrawable(R.styleable.path_openlayoutbackground);
		firstBackground =  ta.getDrawable(R.styleable.path_firstbackground);
		secondBackground =  ta.getDrawable(R.styleable.path_secondbackground);
		thirdBackground =  ta.getDrawable(R.styleable.path_thirdbackground);
		fourthBackground =  ta.getDrawable(R.styleable.path_fourthbackground);

		firstVisibility = ta.getInteger(R.styleable.path_firstfuncvisibile, View.VISIBLE);
		secondVisibility = ta.getInteger(R.styleable.path_secondfuncvisibile, View.VISIBLE);
		thirdVisibility = ta.getInteger(R.styleable.path_thirdfuncvisibile, View.VISIBLE);
		ta.recycle();
	}








	//--------------------------------------------dip和px互相转换
	/**
	 * dip转换成px
	 * @method dip2px
	 * @param context 
	 * @param dpValue
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static int dip2px(Context context, float dpValue) {  
		final float scale = context.getResources().getDisplayMetrics().density;  
		return (int) (dpValue * scale + 0.5f);  

	}

	/**
	 * px转换成dip
	 * @method px2dip
	 * @param context
	 * @param pxValue
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public static int px2dip(Context context, float pxValue) {  
		final float scale = context.getResources().getDisplayMetrics().density;  
		return (int) (pxValue / scale + 0.5f);  
	}





	public void setOnclickListener(PathClickListener pathClickListener){
		this.pathClickListener = pathClickListener;
	}





}

